package org.jeecg.modules.wechat.service.Impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.constant.RedisConstant;
import org.jeecg.modules.wechat.entity.WxUser;
import org.jeecg.modules.wechat.mapper.WxUserMapper;
import org.jeecg.modules.wechat.service.IWxUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @Description: 微信用户表
 * @Author: jeecg-boot
 * @Date:   2020-11-11
 * @Version: V1.0
 */
@Service
public class WxUserServiceImpl extends ServiceImpl<WxUserMapper, WxUser> implements IWxUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private WxUserMapper wxUserMapper;
    private final String onlineDate = "2020-05-03";
    @Override
    public WxUser saveUserByJsonObject(JSONObject userInfo,String openId) {
        WxUser wxUser = new WxUser();
        wxUser.setAvatarUrl(userInfo.getString("avatarUrl"));
        wxUser.setGender(userInfo.getInteger("gender"));
        wxUser.setOpenId(openId);
        wxUser.setNickName(userInfo.getString("nickName"));
        wxUser.setLanguage(userInfo.getString("language"));
        wxUser.setScore(0);
        wxUserMapper.insert(wxUser);
        return wxUser;
    }


    @Override
    public void updateUserScore(String openId, boolean incr, int score) {
        if (incr){
//            redisUtil.incrBy(RedisConstant.USER_SCORE+openId, score);
            wxUserMapper.incrScore(openId,score);
        }else {
//            redisUtil.incrBy(RedisConstant.USER_SCORE+openId, -score);
            wxUserMapper.decrScore(openId,score);
        }
    }



    @Override
    public boolean userSign(String openId) {
        LocalDate beginday = LocalDate.parse(onlineDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate today = LocalDate.now();
        //获取上线之日到今天过了多久，做为redis的offset
        long offset = beginday.until(today, ChronoUnit.DAYS);
        String key = RedisConstant.USER_SIGN+openId;
        boolean flag = redisUtil.setBit(key, offset, true);
        if (!flag){
            // 积分加一
            updateUserScore(openId,true,1);
        }

        return !flag;
    }

    @Override
    public boolean getUserSignStatus(String openId) {
        String key = RedisConstant.USER_SIGN+openId;
        LocalDate beginday = LocalDate.parse(onlineDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        LocalDate today = LocalDate.now();
        //获取上线之日到今天过了多久，做为redis的offset
        long offset = beginday.until(today, ChronoUnit.DAYS);
        return redisUtil.getBit(key,offset);
    }

    @Override
    public int getScoreByOpenId(String openId) {
//        Object o = redisUtil.get(RedisConstant.USER_SCORE + openId);
//        if (!Objects.isNull(o)){
//            return (int)o;
//        }
        WxUser wxUser = wxUserMapper.selectOne(
                new LambdaQueryWrapper<WxUser>()
                        .eq(WxUser::getOpenId, openId));
//        redisUtil.incrBy(RedisConstant.USER_SCORE + openId,wxUser.getScore());
        return wxUser.getScore();
    }

    @Override
    public Page<WxUser> pageWxUser(Page<WxUser> page) {
        return page.setRecords(wxUserMapper.queryWxUser(page));
    }
}
